package xyz.dava.file.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import xyz.dava.file.FileClient;
import xyz.dava.file.impl.aliyun.AliOssFileClient;
import xyz.dava.file.impl.local.LocalFileClient;
import xyz.dava.file.model.VirtualFile;

import java.io.File;
import java.io.InputStream;

public class BaseFileClient implements FileClient {

    private String type;

    private FileClient client;

    private final ApplicationContext context;

    public BaseFileClient(ApplicationContext context) {
        this.context = context;
    }

    public void setType(String type){
        this.type = type;
        switch (type) {
            case "local":
                this.client = context.getBean(LocalFileClient.class);
                break;
            case "aliyun":
                this.client = context.getBean(AliOssFileClient.class);
                break;
        }
    }

    @Override
    public VirtualFile uploadFile(MultipartFile file) {
        return client.uploadFile(file);
    }

    @Override
    public VirtualFile uploadFile(File file) {
        return client.uploadFile(file);
    }

    @Override
    public VirtualFile uploadFile(InputStream is, String key) {
        return client.uploadFile(is, key);
    }

    @Override
    public boolean removeFile(String key) {
        return client.removeFile(key);
    }
}
